package org.zerg.controller.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.zerg.dto.FileDto;
import org.zerg.entity.BannerItem;
import org.zerg.entity.Category;
import org.zerg.service.BannerService;
import org.zerg.service.CategoryService;

@Controller
@RequestMapping("/banner")
public class BannerWebController {

	@Autowired
	private BannerService bannerService;
	
	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	private HttpSession session;
	
	@Autowired
	private HttpServletRequest request;
	
	@RequestMapping("/list")
	public String getBannerList(){
		List<BannerItem> list = bannerService.getBannerList();
		List<Category> list2 = categoryService.categoryAll();
		session.setAttribute("list", list);
		session.setAttribute("list2", list2);
		return "/banner";
	}
	
	@RequestMapping("/modify")
	public String modifyBanner(FileDto dto,@RequestParam("id")int id,@RequestParam("classId")int classId) throws IllegalStateException, IOException{
		if(dto==null||dto.getImgFile()==null||dto.getImgFile().getSize()<=0){
			bannerService.modifyBannerNoFile(id,classId);
		}
		bannerService.modifyBanner(id, dto, classId);
		return "redirect:/banner/list";
	}
	
}
